﻿import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Picker from './Picker'
import Candidates from './Candidates'
import * as acts from '../actions';
 
class Receivers extends Component {
    static propTypes = {
        college: PropTypes.object,
        pickerVisible: PropTypes.bool,
        
        togglePicker: PropTypes.func.isRequired,
        check: PropTypes.func.isRequired,
        expand: PropTypes.func.isRequired,

        depVisible: PropTypes.bool,
        candidates: PropTypes.array.isRequired,
        removeCandidate: PropTypes.func.isRequired,
        clearCandidates: PropTypes.func.isRequired,
        toggleDepVisible: PropTypes.func.isRequired
    }
    render() {
        return (
            <div>
                <Candidates
                    pickerVisible={this.props.pickerVisible}
                    depVisible={this.props.depVisible}
                    candidates={this.props.candidates}
                    removeCandidate={this.props.removeCandidate}
                    clearCandidates={this.props.clearCandidates}
                    togglePicker={this.props.college && this.props.togglePicker}
                    toggleDepVisible={this.props.college && this.props.toggleDepVisible}
                />
                {this.props.pickerVisible &&
                    <Picker
                        college={this.props.college}
                        check={this.props.check}
                        expand={this.props.expand}
                        togglePicker={this.props.togglePicker}
                    />
                }
            </div>
        )
    }
}

const mapState2Props = state => {
    var { pickerVisible = false, depVisible = false, candidates = [], college = null } = state.send;
    return { pickerVisible, depVisible, candidates, college };
};
const mapDispatch2Props = dispatch => ({
    removeCandidate: can => dispatch(acts.removeCandidate(can)),
    clearCandidates: () => dispatch(acts.clearCandidates()),
    check: (dep, sec, t) => dispatch(acts.check(dep, sec, t)),
    expand: (dep, sec) => dispatch(acts.expand(dep, sec)),
    toggleDepVisible: () => dispatch(acts.toggleDepVisible()),
    togglePicker: () => dispatch(acts.togglePicker())
});
export default connect(mapState2Props, mapDispatch2Props)(Receivers);